package com.uenpay.im.ui.old.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

import butterknife.OnClick;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @function 收支明细
 */
public class StatementActivity extends BaseActivity implements RefreshListView.IRefreshListViewListener {

	private String selectedType = "";
	private int page = 1;
	private RefreshListView xListView;
	private StatementAdapter mStatementAdapter;
	private ArrayList<String> types = null;
	private View loading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isFullScreen = true;
		super.onCreate(savedInstanceState);
	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_xlist_layout;
	}

	@Override
	public void initView(View view) {
		initView();
		initListener();
		loading.setVisibility(View.VISIBLE);
		getData();
	}

	protected void initView() {

		((TextView) findViewById(R.id.title_textview)).setText(getResources().getString(R.string.red_balance_payments));
		loading = findViewById(R.id.loading);
		xListView = (RefreshListView) findViewById(R.id.discover_listview);
		xListView.setPullLoadEnable(true);
		xListView.setRefreshListViewListener(this);
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.screen);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
	}

	private void getData() {

		WalletHandle.getPaymentsList(selectedType, page, new WalletHandle.IGetPaymentsListResultListener() {

			@Override
			public void onPaymentsResult(int status, String msg, ArrayList<PaymentEntity> arrayList) {
				myHandler.sendEmptyMessage(0);
				if (status == HttpInvokeResultCode.RESULT_OK) {
					if (page == 1) {
						if (arrayList == null || arrayList.size() < 1) {
							myHandler.sendEmptyMessage(1);
						}
						if (mStatementAdapter == null) {
							mStatementAdapter = new StatementAdapter(StatementActivity.this, arrayList);
							xListView.setAdapter(mStatementAdapter);
						} else {
							mStatementAdapter.setData(arrayList);
						}
						xListView.setPullLoadEnable(true);
					} else {
						if (arrayList != null && arrayList.size() > 0) {
							mStatementAdapter.addData(arrayList);
						} else {
							xListView.setPullLoadEnable(false);
						}
					}

				} else {
					xListView.setPullLoadEnable(false);
					if (!TextUtils.isEmpty(msg)) {
						IMApplication.showToast(msg);
					}
				}
			}
		});

	}

	private Handler myHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {

			if (msg.what == 0) {
				onLoad();
				if (loading != null) {
					loading.setVisibility(View.GONE);
				}
			} else if (msg.what == 1) {
				mStatementAdapter = new StatementAdapter(StatementActivity.this, new ArrayList<PaymentEntity>());
				xListView.setAdapter(mStatementAdapter);
			}
			return false;
		}
	});

	private void onLoad() {
		xListView.stopRefresh();
		xListView.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		page = 1;
		getData();
	}

	@Override
	public void onLoadMore() {
		page++;
		getData();
	}

	protected void initListener() {
		findViewById(R.id.title_right_layout).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (types == null || types.size() < 1) {
					getSubjectList();
				} else {
					showDialog();
				}

			}
		});
		findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				PaymentEntity mRedPaymentEntity = (PaymentEntity) arg0.getAdapter().getItem(arg2);
				if (mRedPaymentEntity != null) {
					Intent intent = new Intent(StatementActivity.this, BillDetailActivity.class);
					intent.putExtra("turnoverid", mRedPaymentEntity.tradeno);
					startActivity(intent);
				}
			}
		});
	}

	private void showDialog() {
		BottomDialogWalletSubject.showBottomDialog(StatementActivity.this, selectedType, types,
				new BottomDialogWalletSubject.IItemOnClick() {

					@Override
					public void onItemClick(String subject) {
						selectedType = subject;
						page = 1;
						loading.setVisibility(View.VISIBLE);
						getData();
					}
				});
	}

	private void getSubjectList() {
		WalletHandle.getSubjectList(new WalletHandle.IGetSubjectResultListener() {

			@Override
			public void onSubjectResult(int status, String msg, ArrayList<String> arrayList) {
				if (status == HttpInvokeResultCode.RESULT_OK) {
					types = arrayList;
					showDialog();
				} else if (!TextUtils.isEmpty(msg)) {
					IMApplication.showToast(msg);
				}
			}
		});
	}
}
